package capstoneproject.dudiachi.Main;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import capstoneproject.dudiachi.Model.OldProvince;

public class OldProvinceGridCellAdapter extends BaseAdapter implements OnClickListener{

	private Context context;
	private ArrayList<OldProvince> list;
	private ArrayList<Bitmap> bitmap;
	private RelativeLayout layout;
	
	public OldProvinceGridCellAdapter(Context context,
			ArrayList<OldProvince> listOldProvince, ArrayList<Bitmap> bitArray) {
		this.context = context;
		this.list = listOldProvince;
		this.bitmap = bitArray;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return bitmap.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		View v = convertView;
		if(v == null){
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.grid_cell, parent, false);
		}
		TextView name = (TextView) v.findViewById(R.id.name);
		name.setText(list.get(position).getOldProvinceName());
		ImageView image = (ImageView) v.findViewById(R.id.image);
		image.setOnClickListener(this);
		image.setTag(""+list.get(position).getOldProvinceID());
		image.setImageBitmap(bitmap.get(position));
		layout = (RelativeLayout) v.findViewById(R.id.layout);
		layout.setTag(""+list.get(position).getOldProvinceID());
		layout.setOnClickListener(this);
		return v;
	}

	@Override
	public void onClick(View v) {
		String tag = (String) v.getTag();
		
		Intent intent = new Intent(context, OldDistrictActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Bundle bundle = new Bundle();
		bundle.putSerializable("listOP", list);
		bundle.putString("id", tag);
		bundle.putString("tag", "fromOldProvince");
		intent.putExtras(bundle);
		context.startActivity(intent);
	}

}
